<?php

namespace App;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Searchable;
    protected $guarded = [];
    protected $fillable = [
        'title', 'content','user_id'
    ];


    //定义索引里的type
    public function searchableAs()
    {
        return "post";
    }
    //定义哪些字段被搜索
    public function toSearchableArray()
    {
        return [
            'title' => $this->title,
            'content' => $this->content,
        ];
    }

//关联用户,模型关联
    public function user()
    {
        return $this->belongsTo('App\User','user_id','id');
    }
 //评论模型
    public function comments()
    {
        return $this->hasMany('App\Comment')->orderBy('created_at','desc');
    }
    //赞 和用户关联
    public function zan($user_id)
    {
        return $this->hasOne(\App\Zan::class)->where('user_id',$user_id);
    }
    //文章的所有赞
    public function zans()
    {
        return $this->hasMany(\App\zan::class);
    }
}
